--[[
    @desc 界面View基类，抽离出一些公共的逻辑，比如加载资源逻辑，打开关闭逻辑
    @author zch
    @data:2022-04-18 11:37:00
]]

require "Common/define"

BaseView = BaseView or BaseClass()
local BaseView = BaseView
local LuaViewm = LuaViewManager

function BaseView:__init()
    self.base_file = ""
    self.layout_file = ""
    self.layer_name = "UI"           --Canvas父节点 Main UI Top
    self.transform = false
    self.gameObject = false
    self.append_queue_ctrl = true    --是否加入界面管理器的队列控制  默认都加入
    self.hide_main_canvas = true     --是否隐藏main的canvas 对于其他canvas才有用
    self.load_callback = false
    self.open_callback = false
    self.close_callback = false
    self.isPop = false               --是否可见
end

function BaseView:Open()
	self.isPop = true
	if self.base_file ~= "" and self.layout_file ~= "" then
	    local function res_load_finash(obj)
	    	self:doCreateWindowView(obj)
	    end
	    LuaViewManager:GetInstance():LoadView(self.base_file, self.layout_file, self.layer_name, res_load_finash)
	end
end

function BaseView:Close()
	if self.is_loaded and self.close_callback then
		self.close_callback()
	end
	LuaViewManager:GetInstance():CloseView(self)
    if self.gameObject then
    	self.gameObject:SetActive(false)
    end
    LuaViewManager:GetInstance():DestroyView(self.gameObject)
end

function BaseView:HasOpen()
	return self.isPop and self.gameObject
end

function BaseView:Hide()
	if self.gameObject then
		self.gameObject:SetActive(false)
		self.wait_for_hide = false
	else
		self.wait_for_hide = true
	end
end

function BaseView:CancelHide()
	self.wait_for_hide = false
	if self.gameObject then
		self.gameObject:SetActive(true)
	end
end

function BaseView:doCreateWindowView(obj)
	if obj == nil then
		self:Close()
		return
	end

	self.gameObject = obj
	self.transform = obj.transform
	self.is_loaded = true
	if self.load_callback then
		self.load_callback()
	end
	BaseView.afterOpen(self)
end

function BaseView:afterOpen()
	if not self.gameObject then
		return
	end

	if self.isPop then
		local function onOpen()
			if self.open_callback then
				self.open_callback()
			end
		end
		onOpen()
		LuaViewManager:GetInstance():OpenView(self)
	else
		self:Close()
	end
end

function BaseView:__delete()
	-- body
end